package scheduler;

import java.util.List;
import java.sql.*;

import com.mysql.jdbc.Driver;

public class MySQLLinkRepository implements ILinkRepository {

        private Connection conn;
        private Statement st;
        ///private Statement st2;
        private String username;
        private String pass;
        private String url;
        //private Boolean ready = false;
        
        public MySQLLinkRepository(String url, String username, String pass)
        {
                this.url = url;
                this.username = username;
                this.pass = pass;
        }
        
        public void init(List<String> linkList) /*tabele FoundURLs i ProcessedURLs w bazie danych net-crawler gotowe*/
        {
                try {
                        new Driver();
                        conn = DriverManager.getConnection(url, username, pass);
                        if (conn!=null)
                        {
                                System.out.println("Database connection established");
                        }
                        st = conn.createStatement();
                        //st2 = conn.createStatement();
                } catch (SQLException e) {
                        e.printStackTrace();//usunąc potem by się nie czepiał
                }
                saveLinks(linkList);
                
        }
        

        @Override
        public synchronized String getLink() {
                String tmp = null;
                ResultSet rs = null;    
                try {
                        st.executeQuery("select url from FoundURLs where processed = 0 limit 1");
                        rs =st.getResultSet();
                        
                        while (!rs.next())
                        {
                                System.out.println("nie znaleziono linków. czekam...");
                                this.wait();
                                //System.out.println("getLink() - przebudzenie");
                                st.executeQuery("select url from FoundURLs limit 1");
                                rs =st.getResultSet();
                        }
                        tmp = rs.getString(1);
                        //System.out.println("getLink(): znalazłem: " + tmp);
                        st.executeUpdate("update FoundURLs set processed = 1 where url='" + tmp + "'");
                        //st.executeUpdate("insert ProcessedURLs(url) values('"+ tmp +"')");
                        //st.executeUpdate("delete from FoundURLs where url='" + tmp + "'");
                        rs.close();
                        
                } catch (SQLException e) {
                        e.printStackTrace();//usunąc potem by się nie czepiał
                } catch (InterruptedException e) {
                        e.printStackTrace();//usunąc potem by się nie czepiał
                }
                //System.out.println("zwracam link");
                return tmp;
        }

        @Override
        public synchronized void saveLinks(List<String> linkList) {
                for (String s:linkList)
                {
                        if(s!=null && s!="")
                        {       
                                try {
                                        st.executeQuery("select url from FoundURLs where url='" + s + "'");
                                        ResultSet rs =st.getResultSet();
                                        if(!rs.next())
                                                st.executeUpdate("insert FoundURLs(url, processed) values('"+ s +"', 0)");
                                } catch (SQLException e) {
                                        //e.printStackTrace();
                                        System.out.println("Link jest za długi");
                                }       
                        }
                }
                //System.out.println("zapisano linki");
                this.notifyAll();
        }

        @Override
        public synchronized void resetRegistry() {
                try {
                	st.executeUpdate("delete from FoundURLs");/*
                        st.executeQuery("select url from FoundURLs where processed = 1");
                        ResultSet rs =st.getResultSet();
                        while(rs.next())
                        {
                                String tmp = rs.getString(1);
                                System.out.println("resetuję - " + tmp);
                                st2.executeUpdate("update FoundURLs set processed = 0 where url='" + tmp + "'");
                                //st.executeUpdate("insert FoundURLs(url, processed) values('"+ s +"', 0)");
                        }*/
                        //st.executeUpdate("INSERT IGNORE INTO FoundURLs SELECT *  FROM ProcessedURLs;");
                } catch (SQLException e) {
                        e.printStackTrace();
                        System.out.println("baza excp");
                }       

        }

        @Override
        public synchronized void destroy() {
                try {
                        st.close();
                        conn.close();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
        }

}
